﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UL360Data;
using UL360SurveyCommon;

namespace UL360SurveyService.Models
{
    public class MailTemplate : UL360SurveyCommon.Models.GenericMailTemplate
    {
        //Replace dynamic value placeholders (#propertyName#) with propertyValues of the "Deltagare" objekt in inputstring
        public static string InsertUserValues(string str, Deltagare d)
        {
            //Get properties of "Deltagare"
            PropertyInfo[] participantProperties = d.GetType().GetProperties();

            //If object has properties
            if (participantProperties.Length > 0)
            {
                //Loop through properties
                foreach (PropertyInfo property in participantProperties)
                {
                    //Only check non-navigationproperties in model
                    if (!property.GetGetMethod().IsVirtual && d.GetType().GetProperty(property.Name).GetValue(d) != null)
                    {
                        //Replace dynamic value placeholder in string with value of property
                        str = str.Replace("#"+ typeof(Deltagare).Name + "." + property.Name + "#",
                            d.GetType().GetProperty(property.Name).GetValue(d).ToString());
                    }
                }
            }

            //Return the resultant string
            return str;
        }

        //Replace dynamic value placeholders (#propertyName#) with propertyValues of the "Kurstillfalle" objekt in inputstring
        public static string InsertCourseValues(string str, Kurstillfalle c)
        {
            //Get properties of "Kurstillfalle"
            PropertyInfo[] courseProperties = c.GetType().GetProperties();

            //If object has properties
            if (courseProperties.Length > 0)
            {
                //Loop through properties
                foreach (PropertyInfo property in courseProperties)
                {
                    //Only check non-navigationproperties in model
                    if (!property.GetGetMethod().IsVirtual && c.GetType().GetProperty(property.Name).GetValue(c) != null)
                    {
                        //Replace dynamic value placeholder in string with value of property
                        str = str.Replace("#" + typeof(Kurstillfalle).Name + "." + property.Name + "#",
                            c.GetType().GetProperty(property.Name).GetValue(c).ToString());
                    }
                }
            }

            //Return the resultant string
            return str;
        }
    }
}
